import hashlib
import pickle as p
import os

def get_md5(s):
    md5=hashlib.md5()
    md5.update(s.encode('utf-8'))
    return md5.hexdigest()
    
def register(username,password):
    with open(dbpath,'rb') as f:
        db=p.load(f)
    db[username]=get_md5(password+username+'the-Salt')
    print('registing succeed!')
    with open(dbpath,'wb') as f:
        p.dump(db,f)

def login(username,password):
    with open(dbpath,'rb') as f:
        db=p.load(f)
    if db[username]==get_md5(password+username+'the-Salt'):
        print('logging succeed!')
    else:
        print('logging faield')

    
if __name__=='__main__':  
    dbpath=r'F:\hash.data'
    if not os.path.exists(dbpath):
        db={}
        with open(dbpath,'wb') as f:
            p.dump(db,f)

    while True:
        op=input('What you want to do,Regist or Login ?\nInput R or L>>>')
        
        if op=="r"or op=='R':
            username=input('Registing,input your name:')
            password=input('Registing,input your password:')
            register(username,password)
        
        elif op=='l'or op=='L':
            username=input('Login,input your name:')
            password=input('Login,input your password:')
            login(username,password)

        else:
            break
